#!/bin/bash

# Install script for Binance Chain Tools
#   - bairdrop
#   - bdumper

# Note: this is based on current structure of `node-binary` repo, which is not optimal
# - The installer script is a hack to simplify the installation process
# - Our binaries should eventually be refactor into a `apt` or `npm` repo, which features upgradability
# - We should not rely on folders for addressing (instead use git branches for versions)

# Detect operating system
# Future Improvement: Refactor into helper function
if [[ "$OSTYPE" == "linux-gnu" ]]; then
  DETECTED_OS="linux"
elif [[ "$OSTYPE" == "darwin"* ]]; then
  DETECTED_OS="mac"
elif [[ "$OSTYPE" == "cygwin" ]]; then
  DETECTED_OS="linux"
elif [[ "$OSTYPE" == "msys" ]]; then
  DETECTED_OS="windows"
elif [[ "$OSTYPE" == "win32" ]]; then
  DETECTED_OS="windows" # TODO(Dan): can you run shell on windows?
elif [[ "$OSTYPE" == "freebsd"* ]]; then
  DETECTED_OS="linux"
else
  FULLNODE_echo "Error: unable to detect operating system. Please install manually by referring to $DOCS_WEB_LINK"
  LIGHTNODE_DOCS_WEB_LINK=""
  exit 1
fi

# Check for existence of wget
if [ ! -x /usr/bin/wget ]; then
  # some extra check if wget is not installed at the usual place
  command -v wget >/dev/null 2>&1 || {
    echo >&2 "Error: you need to have wget installed and in your path. Use brew (mac) or apt (unix) to install wget"
    exit 1
  }
fi


echo "@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@"
echo "@@@@@@@@@@@@@@@@@     @@@@@@@@@@@@@@@@@"
echo "@@@@@@@@@@@@@@@         @@@@@@@@@@@@@@@"
echo "@@@@@@@@@@@@@             @@@@@@@@@@@@@"
echo "@@@@@@@@@@@                 @@@@@@@@@@@"
echo "@@@@@@@@@         @@@         @@@@@@@@@"
echo "@@@@@@@@        @@@@@@@        @@@@@@@@"
echo "@@@@@@@@@@    @@@@@@@@@@@    @@@@@@@@@@"
echo "@@@   @@@@@@@@@@@@   @@@@@@@@@@@@   @@@"
echo "@       @@@@@@@@       @@@@@@@@       @"
echo "@       @@@@@@@@       @@@@@@@@       @"
echo "@@@   @@@@@@@@@@@@   @@@@@@@@@@@@   @@@"
echo "@@@@@@@@@@    @@@@@@@@@@@    @@@@@@@@@@"
echo "@@@@@@@@        @@@@@@@        @@@@@@@@"
echo "@@@@@@@@@         @@@         @@@@@@@@@"
echo "@@@@@@@@@@@                 @@@@@@@@@@@"
echo "@@@@@@@@@@@@@             @@@@@@@@@@@@@"
echo "@@@@@@@@@@@@@@@         @@@@@@@@@@@@@@@"
echo "@@@@@@@@@@@@@@@@@     @@@@@@@@@@@@@@@@@"
echo ""



echo "========== Darkpool Chain Node Installation =========="
echo "Installer Version: 0.1.beta"
echo "Detected OS: $DETECTED_OS"
echo "====================================================="

yum install mc
yum install htop
yum install jq
sudo yum install epel-release
yum install moreutils
yum install leveldb
dnf -y --enablerepo=PowerTools install moreutils
yum install Judy-devel autoconf autoconf-archive autogen automake cmake json-c-devel libmnl-devel libuv-devel lz4-devel
yum install libuv-devel
yum install cmake
yum install npm
#curl https://install.meteor.com/ | sh
#bash <(curl -Ss https://my-netdata.io/kickstart.sh)


# Install location
USR_LOCAL_BIN="/usr/local/bin"
# Note: /usr/local/bin choice from https://unix.stackexchange.com/questions/259231/difference-between-usr-bin-and-usr-local-bin
# Future improvement: needs uninstall script (brew uninstall) that removes executable from bin


GOFILENAME="go1.14.4.linux-amd64.tar.gz"
GODOWNLOADLINK="https://dl.google.com/go/$GOFILENAME"

echo "Ixo Chain Installer"
echo -e "-------------------------\n\n"
sleep 2

if [ "$USER" != "root" ]; then
        echo "You must be logged in as root to use this installer!"
        exit 0;
fi

echo "Starting IXO based install"
sleep 1
echo "Installing dependencies"
apt-get update -y
apt-get upgrade -y
apt-get install -y make gcc curl
rm -r /usr/local/go
rm -r ~/go
cd ~

cd $HOME && curl -O $GODOWNLOADLINK
sleep 1
tar -xvf $HOME/$GOFILENAME

rm $HOME/$GOFILENAME
mv $HOME/go /usr/local

USERNAME=ixo
echo "Creating new IXO system user"
sleep 1

if getent passwd $USERNAME > /dev/null 2>&1; then
	echo "the user exists"
else
	adduser $USERNAME
fi
sleep 1

HOME="/home/$USERNAME"
GOROOT="/usr/local/go"
GOPATH="$HOME/go"
GOBIN="$GOPATH/bin"
GITNAME="tokenchain"
GITREPO="ixo-blockchain"
GITRELEASE="v1.2.1"


su $USERNAME <<EOSU
sleep 1
cd /home/$USERNAME
mkdir /home/$USERNAME/go
sleep 1
export GOROOT="$GOROOT"
export GOPATH="$GOPATH"
export PATH="$PATH:$GOROOT/bin:$GOPATH/bin"
export GO11MODULE=ON
echo "export GOROOT=\"$GOROOT\"" >> $HOME/.bashrc
echo "export GOPATH=\"$GOPATH\"" >> $HOME/.bashrc
echo "export PATH=\"\$PATH:\$GOROOT/bin:\$GOPATH/bin\"" >> $HOME/.bashrc
echo "export GO11MODULE=on" >> $HOME/.bashrc
sleep 1
cd $HOME
sleep 1
mkdir -p $GOPATH/src/github.com/$GITNAME
cd $GOPATH/src/github.com/$GITNAME
git clone https://github.com/$GITNAME/$GITREPO
sleep 1
cd $GITREPO && git fetch --all && git checkout $GITRELEASE
sleep 1
make clean
make distclean
make go-mod-cache
make install
sleep 1
EOSU

DAEMONNAME="ixod"
CLINAME="ixocli"
CHAIN_ID="darkpool-1x"
DAEMON=$GOBIN/$DAEMONNAME
CLI=$GOBIN/$CLINAME

sleep 1

mkdir /home/dxo/.dxod
mkdir /home/dxo/.dxod/config

cp /root/genesis/$CHAIN_ID/genesis.json /home/dxo/.ixod/config/genesis.json

chown -R dxo:dxo /home/dxo/.$DAEMONNAME
chown -R dxo:dxo /home/dxo/.$DAEMONNAME/config/
chown -R dxo:dxo /home/dxo/.$DAEMONNAME/config/genesis.json


su $USERNAME <<EOSU
$DAEMON init "Darkpool node"
EOSU

sleep 5

cp /root/genesis/$CHAIN_ID/genesis.json /home/dxo/.dxod/config/genesis.json

chown -R dxo:dxo /home/dxo/.$DAEMONNAME/config/genesis.json


echo "---"
echo "Your peer ID:"
$DAEMON tendermint show-node-id
echo "---"

cat << EOF > /etc/systemd/system/$DAEMONNAME.service
# /etc/systemd/system/$DAEMONNAME.service
[Unit]
Description=$DAEMONNAME Node
After=network.target
 
[Service]
Type=simple
User=$USERNAME
WorkingDirectory=$HOME
ExecStart=$DAEMON start
Restart=on-failure
RestartSec=3
LimitNOFILE=4096

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload

sleep 3

systemctl enable $DAEMONNAME.service

echo "Service created at /etc/systemd/system/$DAEMONNAME.service."
echo "Run 'systemctl start $DAEMONNAME.service' to start the node"